package cn.tianyigps.tianyigpsapiv2.controller;

import cn.tianyigps.tianyigpsapiv2.param.dto.FenceDTO;
import cn.tianyigps.tianyigpsapiv2.param.dto.PageDTO;
import cn.tianyigps.tianyigpsapiv2.param.enums.ResponseEnum;
import cn.tianyigps.tianyigpsapiv2.param.vo.ResponseVo;
import cn.tianyigps.tianyigpsapiv2.service.FenceService;
import cn.tianyigps.tianyigpspublic.bean.GeoFence;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2019/4/3_18:49
 */
@Slf4j
@RestController
@RequestMapping("/fence")
@CrossOrigin
@Api(value = "FenceController", description = "围栏接口")
public class FenceController {

    @Autowired
    private FenceService fenceService;

    @PostMapping
    @ApiOperation(value = "新增--新增地理位置", notes = "测试接口")
    public ResponseVo insert(@RequestBody FenceDTO geoFenceDTO) {
        if (!geoFenceDTO.checkParam(FenceDTO.SAVE_API_NAME)) {
            return ResponseVo.response(ResponseEnum.ADD_FAILURE);
        }
        try {
            GeoFence geoFence = fenceService.insert(geoFenceDTO);
            return ResponseVo.response(ResponseEnum.ADD_SUCCESS, geoFence);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除--删除一条地理围栏(逻辑删除)", notes = "测试接口")
    public ResponseVo logicRemove(@PathVariable String id) {
        log.info("删除--删除一条地理围栏,入参:{}", id);
        try {
            boolean success = fenceService.logicRemove(id);
            if (success) {
                return ResponseVo.response(ResponseEnum.DELETE_SUCCESS);
            }
            return ResponseVo.response(ResponseEnum.DELETE_FAILURE);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }
    }

    @PutMapping
    @ApiOperation(value = "更新--更新地理围栏", notes = "测试接口")
    public ResponseVo update(@RequestBody FenceDTO geoFenceDTO) {
        try {
            boolean modified = fenceService.update(geoFenceDTO);
            if (!modified) {
                return ResponseVo.response(ResponseEnum.UPDATE_FAILURE);
            }
            return ResponseVo.response(ResponseEnum.UPDATE_SUCCESS);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询--查询地理围栏详情", notes = "测试接口")
    public ResponseVo detail(@PathVariable String id) {
        try {
            GeoFence geoFence = fenceService.detail(id);
            if (Optional.ofNullable(geoFence).isPresent()) {
                return ResponseVo.response(ResponseEnum.QUERY_SUCCESS, geoFence);
            }
            return ResponseVo.response(ResponseEnum.QUERY_NULL);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }
    }

    @GetMapping
    @ApiOperation(value = "查询--查询指定地理围栏列表", notes = "测试接口")
    public ResponseVo list(@RequestParam String group, @RequestParam int page, @RequestParam int limit) {
        try {
            PageDTO pageDTO = fenceService.getGeoFenceList(group, page, limit);
            return ResponseVo.response(ResponseEnum.QUERY_SUCCESS, pageDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }
    }

    @GetMapping("/group")
    @ApiOperation(value = "查询--查询地理位置分组名", notes = "测试接口")
    public ResponseVo listGroup(@RequestParam int page, @RequestParam int limit) {
        try {
            Set groupSet = fenceService.getGroupNameSet(page, limit);
            return ResponseVo.response(ResponseEnum.QUERY_SUCCESS, groupSet);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseVo.response(ResponseEnum.ERROR);
        }
    }

}
